package cn.miju.ser.framework.config;

import cn.miju.database.entity.Merchant;
import cn.miju.database.service.MerchantDataService;
import cn.miju.ser.framework.constant.SerConstant;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * <pre>
 * 商户信息启动时写入缓存
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-17 10:03
 */
@Component
@RequiredArgsConstructor
public class MerchantStartCacheConfig {
    private final MerchantDataService merchantDataService;
    private final StringRedisTemplate redisTemplate;

    @PostConstruct
    public void invoke() {
        List<Merchant> merchantList = merchantDataService.list();
        if (!CollectionUtils.isEmpty(merchantList)) {
            merchantList.forEach(merchant -> {
                redisTemplate.opsForValue().set(SerConstant.Redis.MERCHANT_INFO + merchant.getId(), JSONObject.toJSONString(merchant));
                redisTemplate.opsForValue().set(SerConstant.Redis.MERCHANT_INFO + merchant.getPlatformMerchantNo(), JSONObject.toJSONString(merchant));
            });
        }
    }
}
